
var pandianid = GetQueryString('pandianid');
var shopcode = sessionStorage.getItem("shopcode");
var userId = sessionStorage.getItem("userId");
function ICalendarClick(){
	
}
$(document).ready(function(){
/*	alert(sessionStorage.getItem("reload"));*/
    if(sessionStorage.getItem("reload")=='true'){
	     window.location.reload();
		sessionStorage.setItem("reload", false);
	}else{
		getPandianJieguoBiaozhun();
		
		$('#name').text(sessionStorage.getItem("name"));
		$('#saveButton').on('click',saveInfo);
	}
	
	
});
function getPandianJieguoBiaozhun(){
	showloading();
	var sendData = {};
	sendData.flag = 0;
	$.ajax({
		url: "getPandianJieguoBiaozhun"
		,type:'post'
		,dataType:'json'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify(sendData) 
		,success: function(res){
			console.log(res)
			try {
				if(res.success){
					var data = res.data;
					$('#f_div').empty();
					if(data != undefined && data.length > 0){
						addPandianJieguoBiaozhun(data);
						//填充数据
						addPandianJieguoBiaozhunValue();
					}
				}else{
					layer.msg(res.message);
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
		}
    });
}
function saveInfo(){
	showloading();
	try {
		if(shopcode == null || shopcode == '' || shopcode == 'null'){
			layer.alert("没有获取到门店号，请退出后重试");
		}else{
			var sendData = {};
			var insertArray = [];
			var checkFlag = true;
			$('div[itemDiv]').each(function(i,obj){
				if(checkFlag){
					var insertJson = {};
					$(obj).find('[jsonKey]').each(function(j,el){
						if($(el).parent().find('span[bitian]').text() == '*' && $(el).val() == ''){
							layer.msg($(el).parent().find('h5 span').text()+"为必填项");
							checkFlag = false;
							return false;
						}else{
							var key = $(el).attr('jsonKey');
							insertJson[key] = $(el).val();
							//console.log(insertJson);
							key = null;
						}
					});
					insertJson.shopcode = shopcode;
					insertArray.push(insertJson);
				}else{
					return false;
				}
			});
			sendData.dataArray = insertArray;
			console.log(sendData);
//			alert(checkFlag)
			if(checkFlag && insertArray.length > 0){
				$.ajax({
					url: "../pandianevery/replacePandianJianchaChanggui"
					,type:'post'
					,dataType:'json'
					,async: false
					,contentType: "application/json; charset=utf-8"
					,data:JSON.stringify(sendData) 
					,success: function(res){
						console.log(res)
						if(res.success){
//							history.go(-1);
							window.location.href='toJiancha_index_list?pandianid='+pandianid;
						}else{
							hideloading();
							layer.alert(res.message);
						}
					}
			    });
			}
		}
		
/*		console.log(insertArray.length);*/
	} catch (e) {
		console.log(e)
	} finally{
		hideloading();
	}	
	
}	

//填充标准查询数据
function addPandianJieguoBiaozhunValue(){
	var sendData = {};
	sendData.pandianid = pandianid;
	$.ajax({
		url: "../pandianevery/getPandianJianchaChanggui"
		,type:'post'
		,dataType:'json'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify(sendData) 
		,success: function(res){
			console.log(res)
			$('input[jsonKey="biaozhunvalue"]').val('');
			try {
				if(res.success){
					var data = res.data;
					for (var i = 0; i < data.length; i++) {
						var biaozhun = data[i].biaozhun;
						$('div[itemDiv="'+biaozhun+'"]').find('input[jsonKey="biaozhunvalue"]').val(data[i].biaozhunvalue);
					}
				}else{
					layer.msg(res.message);
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
		}
    });
}

function addPandianJieguoBiaozhun(jsonparse){
	var trStr='';
	for(var i = 0 ;i<jsonparse.length;i++){
		trStr += '		<div class="widget-box add_mar_tb7p" itemDiv="'+jsonparse[i].id+'">'
				+'			<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey add_fle">'
				+'             <div class="add_fle_start">'
				+'				<h5><i class="iconfont">&#xe600;</i>&nbsp;<span>'+jsonparse[i].itemname+'</span><span bitian class="red">*</span></h5></div>'
				+'				<input type="text" placeholder="请输入'+jsonparse[i].itemname+'" jsonKey="biaozhunvalue" autocomplete="off"   class="pull-right add_bor_none" style="flex-grow:1; white-space:normal; word-bredk:break-all;"/>'
				+'				<input type="hidden" value="'+jsonparse[i].id+'" jsonKey="biaozhun"/>'
				+'				<input type="hidden" value="'+jsonparse[i].itemname+'" jsonKey="biaozhunname"/>'
//				+'				<input type="hidden" value="'+shopcode+'" jsonKey="shopcode"/>'
				+'				<input type="hidden" value="'+pandianid+'" jsonKey="pandianid"/>'	
				+'				<input type="hidden" value="'+userId+'" jsonKey="lrr"/>'	
				+'			</div>'
				+'		</div>';
	}
	$('#f_div').append('<div class="col_box" >'
					+trStr	
					+'</div>');
}



